﻿using AgencyManagerSystem.Model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgencyManagerSystem.DataFetcher
{
    public class StockFetcher : ErpFetcher<StockModel>
    {
        public StockFetcher() : base()
        {
             
        }
        public override void AddDefaultFilter(StringBuilder stringBuilder, DynamicParameters dynamicParameters)
        {
            if (userProfile == null)
                return;
            //准备加入默认的过滤条件
            string keyStr = String.Format("@{0}_{1}", "CustCode", COUNTER++);
            stringBuilder.AppendFormat(" AND CustCode = {0} ", keyStr);
            dynamicParameters.Add(keyStr, userProfile.UserCode);
        }
        public StockGroupModel GetByProductionAndCustomer(int prod,string customer)
        {
            Condition[] conditions = new Condition[] {
                new Condition() { Key = "Production", Op = "=", Value = prod },
                new Condition(){ Key="CustCode",Op="=",Value=customer }
            };
            var list = List(conditions);
            if (list.Count > 0)
            {
                StockGroupModel result = new StockGroupModel();
                
                foreach(StockModel stock in list)
                {
                    result.Production = stock.Production;
                    result.CustCode = stock.CustCode;
                    result.Qty += stock.Qty;
                    result.AvaQty += stock.AvaQty;
                }
                return result;
            }
            else
                return null;
        }
    }
}
